package com.android.nodejs.livejs;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import android.app.Application;

import com.android.nodejs.livejs.models.MyMessage;
import com.android.nodejs.livejs.models.User;

public class MyApplication extends Application {
	public static String PACKAGE_NAME;
	public static String FOLDER_NAME;
	public static int API_VERSION;
	/** Current user signed in to the system */
	public static User USER_CURRENT = null;
	/** Current socket  */
	public static Socket SOCKET_CURRENT = null;
	/** Current input stream */
	public static InputStream IS_CURRENT = null;
	/** Current output stream */
    public static OutputStream OS_CURRENT = null;
    /** list friends of current user */
    public static List<User> LIST_FRIENDS = null;
    /** list friends request of current user */
    public static List<User> LIST_FRIENDS_REQUEST = new ArrayList<User>();
    /** list message offline of current user */
    public static List<MyMessage> LIST_MESSAGE_OFFLINE;
    /** list message of current user */
    //public static ArrayList<MyMessage> MSGS = new ArrayList<MyMessage>();
    public static HashMap<Integer, MyMessage> MSGS = new LinkedHashMap<Integer, MyMessage>();
    /** 
     * this variable for check isRunning of ChatActivity </br>
     * String: key is id_friend </br>
     * boolean: value is true if activity runnning, flase is not running
     */
    public static HashMap<String, Boolean> IS_RUNNING = new DefaultHashMap<String, Boolean>(false);
    /** group id when chat group */
    public static String GROUP_ID;
    /** list id of group */
    public static HashMap<Integer, List<Integer>> LIST_ID_EACH_GROUP = new LinkedHashMap<Integer, List<Integer>>();
	
	@Override
	public void onCreate() {
		super.onCreate();
		initVariables();
	}

	protected void initVariables() {
		PACKAGE_NAME = getPackageName();
		FOLDER_NAME = "SFood";
		API_VERSION = android.os.Build.VERSION.SDK_INT;
	}
	
	/**
     * overide HashMap has a default value
     * 
     * @author vuvt
     *
     * @param <K>
     * @param <V>
     */
    @SuppressWarnings("serial")
    private static class DefaultHashMap<K, V> extends HashMap<K, V> {
        protected V defaultValue;

        public DefaultHashMap(V defaultValue) {
            this.defaultValue = defaultValue;
        }

        @Override
        public V get(Object k) {
            V v = super.get(k);
            return ((v == null) && !this.containsKey(k)) ? this.defaultValue : v;
        }
    }
}